﻿#include "qantenna.h"
#include <QDebug>
#include <QTimer>

QAntenna::QAntenna(QWidget *parent) : QWidget(parent)
{
line1=true;
line2=false;
line3=true;
line4=false;
globadata=GlobalData::GetInstance();
this->wid=parent;
widget= (QWidget*)(wid->parent());
}

void QAntenna::paintEvent(QPaintEvent *)
{  point_width=rect().height();
    //qDebug()<<"宽度："<<point_width;
    m_width=this->width();
    m_height=this->height();
    center1=QPoint(m_width/2,m_height/4);
    center2=QPoint(m_width/2,m_height/4*3);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);// 抗锯齿
    drawRound(painter);
    drawLine(painter);
    drawCurve(painter,ZD);
    drawCurve1(painter,ZDB);
}
//mouseDoubleClickEvent  mousePressEvent
void QAntenna::mouseReleaseEvent(QMouseEvent* e)
{
    //QTimer::singleShot(100,this,[=](){});
    if(isContains(e->pos()))
    {
       //ZDPath();
       isLeft(mapToParent(e->pos()));
    }
    if(isContainsB(e->pos()))
    {
      //  ZDBPath();
        isRight(mapToParent(e->pos()));
    }

    //qDebug()<<"wid"<<widget->width()<<"wid1"<<this->width();
}

void QAntenna::ZDPath()
{
    ZD=!ZD;
    line1=!line1;
    line2=!line2;
    update();
}

void QAntenna::ZDBPath()
{
    ZDB=!ZDB;
    line3=!line3;
    line4=!line4;
    update();
}

void QAntenna::drawRound(QPainter &painter)//画圆
{

    painter.save();
    QPen pen(QColor(100,100,140));
    pen.setWidth(point_width/41);
    painter.setPen(pen);
    painter.drawEllipse(QPointF(m_width/2,m_height/4),m_width/16,m_width/16);
    painter.drawEllipse(QPointF(m_width/2,m_height/4*3),m_width/16,m_width/16);
    painter.restore();
}

void QAntenna::drawLine(QPainter &painter)//画线
{
    painter.save();
    QPen pen(QColor(100,100,140));
    pen.setWidth(point_width/41);
    drawLine1(painter,line1);
    drawLine2(painter,line2);
    drawLine3(painter,line3);
    drawLine4(painter,line4);
    pen.setWidth(point_width/41);
    painter.setPen(pen);
    double line=m_height*4+m_width;
    double lineL=m_height*12-m_width;
    pen.setColor(Qt::green);
    painter.setPen(pen);
    painter.drawLine(QPointF(m_width/2,line/16),QPointF(m_width/2,lineL/16));
    painter.restore();
    //painter.drawLine();
}


void QAntenna::drawCurve(QPainter &painter,bool a)//画圆弧
{
    bool ZD1=a;
    painter.save();
    QPen pen(QColor(100,100,140));
    pen.setWidth(point_width/41);
    //painter.drawPie();
    int BLTopY=m_height*4-m_width;//左上角y轴坐标
    int BRbomY=m_height*4+m_width;//右下角y轴坐标
    painter.setClipRegion(QRegion(m_width/16*7, BLTopY/16, m_width/8, m_width/8));
    painter.setPen(pen);
    if(ZD1==false)
    {
        painter.drawEllipse(QPointF(m_width/16*7, BLTopY/16),m_width/16,m_width/16);
        pen.setColor(Qt::green);
        painter.setPen(pen);
        painter.drawEllipse(QPointF(m_width/16*9, BRbomY/16),m_width/16,m_width/16);
        emit Light(1);
    }
    else if(ZD1==true)//初始
    {   pen.setColor(Qt::green);
        painter.setPen(pen);
        painter.drawEllipse(QPointF(m_width/16*7, BRbomY/16),m_width/16,m_width/16);
        pen.setColor(QColor(100,100,140));
        painter.setPen(pen);
        painter.drawEllipse(QPointF(m_width/16*9, BLTopY/16),m_width/16,m_width/16);
        emit Light(2);
    }
    painter.restore();
}

void QAntenna::drawCurve1(QPainter &painter, bool a)//圆弧下
{
    bool ZD1=a;
    painter.save();
    QPen pen(QColor(100,100,140));
    pen.setWidth(point_width/41);
    painter.setPen(pen);
    int BLTopY=m_height*12-m_width;//左上角y轴坐标
    int BRbomY=m_height*12+m_width;//右下角y轴坐标
    painter.setClipRegion(QRegion(m_width/16*7, BLTopY/16, m_width/8, m_width/8));
    if(ZD1==true)
    {  pen.setColor(QColor(100,100,140));
        painter.setPen(pen);
        painter.drawEllipse(QPointF(m_width/16*9, BRbomY/16),m_width/16,m_width/16);
        pen.setColor(Qt::green);
        painter.setPen(pen);
        painter.drawEllipse(QPointF(m_width/16*7, BLTopY/16),m_width/16,m_width/16);//red
    }
    else if(ZD1==false)
    {    pen.setColor(QColor(100,100,140));
        painter.setPen(pen);
        painter.drawEllipse(QPointF(m_width/16*7, BRbomY/16),m_width/16,m_width/16);
        pen.setColor(Qt::green);
        painter.setPen(pen);
        painter.drawEllipse(QPointF(m_width/16*9, BLTopY/16),m_width/16,m_width/16);//red

    }
    painter.restore();
}

bool QAntenna::isContains(QPoint p)
{
    return(center1.x()-m_width/16<p.x()
         &&center1.x()+m_width/16>p.x()
         &&center1.y()-m_width/16<p.y()
         &&center1.y()+m_width/16>p.y());
}

bool QAntenna::isContainsB(QPoint p)
{
    return(center2.x()-m_width/16<p.x()
         &&center2.x()+m_width/16>p.x()
         &&center2.y()-m_width/16<p.y()
           &&center2.y()+m_width/16>p.y());
}

void QAntenna::isLeft(QPoint p)
{
    int width=widget->width();
    if(width/2>p.x())
    {
        globadata->Tower_50=!(globadata->Tower_50);
       emit globadata->TcpJX();
    }
    else if(width/2<p.x())
    {
        globadata->Tower_10=!(globadata->Tower_10);
        emit globadata->TcpZY();
    }
    //emit globadata->Tcp();
}

void QAntenna::isRight(QPoint p)
{
    int width=widget->width();
    if(width/2>p.x())
    {
        globadata->Computer_50=!(globadata->Computer_50);
       emit globadata->TcpJX();//发送信号，发射机发射指令
    }
    else if(width/2<p.x())
    {
        globadata->Computer_10=!(globadata->Computer_10);
       emit globadata->TcpZY();
    }
   // emit globadata->Tcp();
}

void QAntenna::drawLine1(QPainter &painter, bool a)
{
    QPen pen(a?Qt::green:QColor(100,100,140));
    pen.setWidth(point_width/41);
    painter.setPen(pen);
    painter.drawLine(QPointF(m_width/4,0),             QPointF(m_width/4,m_height/4));//左上
    painter.drawLine(QPointF(m_width/4,m_height/4),    QPointF(m_width/16*7,m_height/4));//左上
}

void QAntenna::drawLine2(QPainter &painter, bool a)
{
    QPen pen(a?Qt::green:QColor(100,100,140));
    pen.setWidth(point_width/41);
    painter.setPen(pen);
    painter.drawLine(QPointF(m_width/4*3,0),           QPointF(m_width/4*3,m_height/4));//右上
    painter.drawLine(QPointF(m_width/16*9,m_height/4), QPointF(m_width/4*3,m_height/4));//右上
}

void QAntenna::drawLine3(QPainter &painter, bool a)
{
    QPen pen(a?Qt::green:QColor(100,100,140));
    pen.setWidth(point_width/41);
    painter.setPen(pen);
    painter.drawLine(QPointF(m_width/4,m_height),      QPointF(m_width/4,m_height/4*3));//左下
    painter.drawLine(QPointF(m_width/4,m_height/4*3),  QPointF(m_width/16*7,m_height/4*3));//左下
emit work(a);
}

void QAntenna::drawLine4(QPainter &painter, bool a)
{
    QPen pen(a?Qt::green:QColor(100,100,140));
    pen.setWidth(point_width/41);
    painter.setPen(pen);
    painter.drawLine(QPointF(m_width/4*3,m_height),    QPointF(m_width/4*3,m_height/4*3));//右下
    painter.drawLine(QPointF(m_width/4*3,m_height/4*3),QPointF(m_width/16*9,m_height/4*3));//右下
//emit work(a);
}

void QAntenna::updateLine(QString Kw, bool a, bool b)//a为true,主塔工作，左上线绿色，圆盘初始。b为true时，主机工作，左下线绿色，圆盘初始
{
    if(Kw=="50Kw")
    {
       globadata->Tower_50=a;
       globadata->Computer_50=b;
    }
    if(Kw=="10Kw")
    {
        globadata->Tower_10=a;
        globadata->Computer_10=b;
    }
    line1=a;
    line2=!a;
    line3=b;
    line4=!b;
    ZD=a;
    ZDB=b;
    int light=2;
    if(a==true)
    {
        emit Light(light);
    }
    else if(a==false){
        light=1;
        emit Light(light);
    }
    emit work(b);
    update();
}

void QAntenna::defeatedJX()//天线设置失败时，恢复天线参数
{
    globadata->Computer_50=line3;
    globadata->Tower_50=line1;
}

void QAntenna::defeatedZY()
{
    globadata->Computer_10=line3;
    globadata->Tower_10=line1;
}
